package codegenerate.popup.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.grom.constant.FileUtil;
import com.grom.velocity.ActionCreator;
import com.grom.velocity.DaoCreator;
import com.grom.velocity.JspCreator;
import com.grom.velocity.PoCreator;
import com.grom.velocity.ServiceCreator;
import com.grom.xml.XmlParser;

public class CodeGenerate implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public CodeGenerate() {
		super();
	}

	/**
	 * 这个方法会在有动作的时候被调用
	 * 
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		System.out.println("start");
		System.out.println(action.getDescription());
		XmlParser parser = new XmlParser(FileUtil.getSelectedStream());
		if (action.getText().equals("Generate All")) {
			new DaoCreator(parser).createFile("dao.vm");
			new DaoCreator(parser).createFile("daoImpl.vm");
			new ActionCreator(parser).createFile("action.vm");
			new ServiceCreator(parser).createFile("service.vm");
			new JspCreator(parser).createFile("jsp.vm");
			new PoCreator(parser).createFile("po.vm");
		}
		MessageDialog.openInformation(shell, "Finished", "Generate finished, please refresh your project!" );

	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
